package com.atguigu.flinkSql;

import com.atguigu.been.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

/**
 * @author wky
 * @create 2021-07-21-9:21
 */
//查询未注册的表
public class Flink07_Sql_select_nolongin {
    public static void main(String[] args) throws Exception {
        //创建流执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<WaterSensor> streamSource = env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                new WaterSensor("sensor_1", 2000L, 20),
                new WaterSensor("sensor_2", 3000L, 30),
                new WaterSensor("sensor_1", 4000L, 40),
                new WaterSensor("sensor_1", 5000L, 50),
                new WaterSensor("sensor_2", 6000L, 60));
        // 创建表执行环境
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);
        // TODO SQL写法
        //创建表
        Table table = tableEnvironment.fromDataStream(streamSource);
//        //将表注册为临时视图
//        tableEnvironment.createTemporaryView("sensor",table);
        //在临时视图查询数据
        Table sqlQuery = tableEnvironment.sqlQuery("select * from  "+table+" where id='sensor_1'");
        //sqlQuery.execute().print();
        tableEnvironment.toAppendStream(sqlQuery,Row.class).print();
        env.execute();

    }
}
